home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10786 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  77 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Initializing each element in an array of classes
  5. Message-ID: <marnoldDo1KtL.C1A@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4hssvm$178_001@basmith1.iquest.net> <Do1A90.33L@presby.edu>
  8. Date: Sun, 10 Mar 1996 07:58:33 GMT
  9. Sender: marnold@netcom13.netcom.com
  10.  
  11. jtbell@presby.edu (Jon Bell) writes:
  12.  
  13. > Brian Smith <basmith@iquest.net> wrote:
  14. >>class Dot {
  15. >>  int X;
  16. >>  int Y;
  17. >>  Dot(void);
  18. >>  Dot(int InitX, int InitY, int InitSize = 1) // default for InitSize is 1
  19. >>};
  20.  
  21. >>class Box {
  22. >>  int i;
  23. >>  int j;
  24. >>  Dot DotArray[3];
  25. >>  etc...
  26. >>}
  27.  
  28. >>How can I create this array of Dots with the X and Y values of, for example, 
  29. >>(1,1), (2,2), and (3,3)? The Borland Turbo C++ manual is of no help at all.
  30.  
  31. By writing the ctor of Box like so...
  32.  
  33.    Box::Box():
  34.       i(/* some value for i */),
  35.       j(/* some value for j */)
  36.       {
  37.       DotArray[0] = Dot(1, 1);
  38.       DotArray[1] = Dot(2, 2);
  39.       DotArray[2] = Dot(3, 3);
  40.       }
  41.  
  42. There is no way to initialize an array member in the intializer list (as can
  43. be done for the int members i and j, as I showed).  You will have to set the 
  44. initial value of each array element in the ctor's body.
  45.  
  46. An alternative to a built-in array of Dots is to the vector template class 
  47. from the Standard Template Library (STL).  vector has a constructor than can 
  48. take another vector to intialize itself with (basically, it just copies the 
  49. contents of the other vector).  You could then intialize a vector member using 
  50. some vector passed to box, or perhaps some default vector sitting around 
  51. solely for the purpose of intializing the vector member of each instance of 
  52. Box.  Since vectors are single C++ objects, you can intialize them in the 
  53. intializer list...
  54.  
  55.   #include "vector.h"   // from STL
  56.  
  57.   class Box 
  58.       {
  59.       vector<Dot> DotArray;
  60.  
  61.       public:
  62.          Box(const vector<Dot>& InitialDots):
  63.             DotArray(InitialDots)
  64.             {
  65.             }
  66.       };
  67.  
  68.  
  69. Regards,
  70. -------------------------------------------------------------------------
  71. Matt Arnold                       |        | ||| | |||| |  | | || ||
  72. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  73. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  74. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  75. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  76. -------------------------------------------------------------------------
  77.